import 'package:environmental_les_app/common/style/colours.dart';
import 'package:environmental_les_app/common/widgets/button_public_rect.dart';
import 'package:environmental_les_app/common/widgets/exit_widget.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';
import 'index.dart';
import 'widgets/input_item.dart';

class LoginPage extends GetView<LoginController> {
  const LoginPage({super.key});

  // 主视图
  Widget _buildView() {
    return SingleChildScrollView(
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 30.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            vGap20,
            // logo
            Container(
              color: Colours.appMain,
              height: 42,
              width: 42,
            ),
            vGap10,
            const Row(
              children: [
                Text("您好，欢迎使用",
                    style: TextStyle(fontSize: 19, color: Colors.black)),
              ],
            ),
            vGap10,
            const Row(
              children: [
                Text("移动执法系统",
                    style: TextStyle(
                        fontSize: 24,
                        color: Colors.black,
                        fontWeight: FontWeight.w500)),
              ],
            ),
            vGap40,
            InputItem(
              controller: controller.phoneController,
              focusNode: controller.phoneFocusNode,
              hintText: '手机号',
              maxLength: 11,
              type: 1,
            ),
            vGap20,
            Visibility(
              visible: controller.isPwdLogin,
              child: InputItem(
                controller: controller.pwdController,
                focusNode: controller.pwdFocusNode,
                hintText: '密码',
                maxLength: 16,
                type: 2,
              ),
            ),
            Visibility(
              visible: !controller.isPwdLogin,
              child: InputItem(
                controller: controller.vCodeController,
                focusNode: controller.vCodeFocusNode,
                phoneController: controller.phoneController,
                vCodeType: "captchaUserLogin",
                hintText: '验证码',
                maxLength: 6,
                type: 3,
              ),
            ),
            vGap60,
            Align(
              alignment: Alignment.centerRight,
              child: InkWell(
                onTap: controller.isPwdLogin ? controller.onForgetPwd : null,
                child: ConstrainedBox(
                  constraints: const BoxConstraints(minHeight: 25.0),
                  child: Text(
                    controller.isPwdLogin ? "忘记密码?" : "",
                    style: const TextStyle(
                        fontSize: 14.0,
                        color: Colours.color989FA8,
                        fontWeight: FontWeight.bold),
                  ),
                ),
              ),
            ),
            vGap4,
            ButtonPublicRect(
              text: "登 录",
              onPressed: controller.onLogin,
              minWidth: double.infinity,
              throttle: true,
            ),
            vGap14,
            InkWell(
              onTap: controller.onChangeLoginType,
              child: Align(
                alignment: Alignment.center,
                child: Text(
                  controller.isPwdLogin ? "密码登录" : "验证码登录",
                  style: const TextStyle(
                      color: Colours.color989FA8,
                      fontSize: 16.0,
                      fontWeight: FontWeight.bold),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return ExitWidget(
      child: GetBuilder<LoginController>(
        init: LoginController(),
        id: "login",
        builder: (_) {
          return Scaffold(
            body: Stack(
              children: [
                Container(
                  height: 266,
                  decoration: BoxDecoration(
                      image: DecorationImage(
                    image: AssetImage(ImageUtils.getImgPath("bg_login_top")),
                    fit: BoxFit.cover,
                  )),
                ),
                SafeArea(
                  child: SingleChildScrollView(child: _buildView()),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}
